package org.compression.services.imp;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.compression.mapper.BookMagamentMapper;
import org.compression.services.BookMagamentService;
import org.compression.util.RedisUtil;
import org.model.auth.PageUser;
import org.model.business.AddModel;
import org.model.business.BookMagament;
import org.model.comm.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Service
@Transactional
public class BookMagamentServiceImp implements BookMagamentService{

	@Autowired
	BookMagamentMapper mBookMagamentMapper;
	

	@Autowired
	RedisUtil	mRedisUtil;
	
	
	
	@Value("${file.path}")
	public String filepath;

	@Value("${file.pathImage}")
	public String pathImage;
	
	
	@Override
	public Object getMember(Page mPage) {
		PageUser m=new PageUser();
		Object date=(Object) mRedisUtil.get(this.getClass().getName()+"."+this.getClass().getDeclaredMethods()[0].getName()+"args:"+mPage.getCurrentPageNum()+mPage.getPageSize());
		System.out.println(this.getClass().getName()+"."+this.getClass().getDeclaredMethods()[0].getName()+"args:"+mPage.getCurrentPageNum()+mPage.getPageSize());
		if(date!=null) {
			return date;
		}else {
			m.setMember(mBookMagamentMapper.getPageBookMagament(mPage));
			m.setTotal(mBookMagamentMapper.getTotal());
			mRedisUtil.set(this.getClass().getName()+"."+this.getClass().getDeclaredMethods()[0].getName()+"args:"+mPage.getCurrentPageNum()+mPage.getPageSize(), m);
		}
		return m;
	}
	@Override
	public Object selectName(BookMagament mBookMagaments) {
		return mBookMagamentMapper.selectName(mBookMagaments);
	
	}
	
	@Override
	public Object insertBook(AddModel mBookMagament) {
		Page mPage =mBookMagament.getmPage();
		System.out.println(this.getClass().getName()+"."+this.getClass().getDeclaredMethods()[0].getName()+"args:"+mPage.getCurrentPageNum()+mPage.getPageSize());
		mRedisUtil.del(this.getClass().getName()+"."+this.getClass().getDeclaredMethods()[0].getName()+"args:"+mPage.getCurrentPageNum()+mPage.getPageSize());
		String uuid=UUID.randomUUID().toString();
		if(mBookMagamentMapper.selectUUID(uuid).size()<=0) {
		}else {
			 uuid=UUID.randomUUID().toString();
		}
		mBookMagament.getmBookMagament().setAttr(uuid);
		return mBookMagamentMapper.insertBookMagamentDate(mBookMagament.getmBookMagament());
	}
	
	@Override
	public Object selectCreateTime(String mBookMagament) {
		return mBookMagamentMapper.selectCreateTime(mBookMagament);
	}
	@Override
	public Object insertBook(BookMagament mBookMagament) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Object insertBook(MultipartFile file,String  createdate) {
		System.out.println(createdate);
		if (!file.isEmpty()) {
			try {
				BufferedOutputStream out = new BufferedOutputStream(
						new FileOutputStream(new File(filepath+file.getOriginalFilename())));
				out.write(file.getBytes());
				out.flush();
				out.close();
				List<Integer> ids=mBookMagamentMapper.selectCreateTime(createdate);
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			} catch (IOException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			}
			return "上传成功";
		} else {
			return "上传失败，因为文件是空的.";
		}
	}
	
	
	
}
